/*******************************************************************************
* @file    fins_define.h
* @author  Fins
* @brief   Common definition
* @time    2020-11-13 19:03:47 Friday
* @codeing UTF-8
* @license
*     Copyright 2020 Fins
*
*     Licensed under the Apache License, Version 2.0 (the "License");
*     you may not use this file except in compliance with the License.
*     You may obtain a copy of the License at
*
*         http://www.apache.org/licenses/LICENSE-2.0
*
*     Unless required by applicable law or agreed to in writing, software
*     distributed under the License is distributed on an "AS IS" BASIS,
*     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*     See the License for the specific language governing permissions and
*     limitations under the License.
* @verbatim
================================================================================
                        ##### xxxxxxxxxxxxxxxxxxxxxxx #####
================================================================================
* @endverbatim
* @attention
*******************************************************************************/



/** Prevent recursive inclusion */
#ifndef __FINS_DEFINE_H
#define __FINS_DEFINE_H

/** C++ compatible */
#ifdef __cplusplus
 extern "C" {
#endif /** __cplusplus */

/* Includes start *************************************************************/
/*     Lib files ****************************** */

/*     User files ***************************** */


/* Includes end ***************************************************************/

/* Exported define start ******************************************************/

/*************************************************
 * @brief Rename basic type
 ************************************************/
typedef unsigned long long     fuint64;   /**< unsigned int 64 */
typedef unsigned int           fuint32;   /**< unsigned int 32 */
typedef unsigned short         fuint16;   /**< unsigned int 16 */
typedef unsigned char          fuint8;    /**< unsigned int 08 */

typedef signed long long       fsint64;   /**< signed int 64   */
typedef signed int             fsint32;   /**< signed int 32   */
typedef signed short           fsint16;   /**< signed int 16   */
typedef signed char            fsint8;    /**< signed int 08   */

typedef signed char            fchar;     /**< char            */
typedef unsigned char          fuchar;    /**< unsigned char   */

typedef float                  ffloat32;  /**< signed float 32 */
typedef double                 ffloat64;  /**< signed float 64 */

typedef fuint8                 fbool;     /**< boolean         */

typedef fuint8                 fstate;    /**< state           */

typedef fuint32                faddr;     /**< addr            */



/*************************************************
 * @brief Word size
 ************************************************/
#define F_WORDSIZE             (sizeof(int*))



/*************************************************
 * @brief Boolean define
 ************************************************/
#define F_TRUE                 1            /**< boolean true    */
#define F_FALSE                0            /**< boolean false   */



/*************************************************
 * @brief State define
 ************************************************/
#define F_ENABLE                 1            /**< state enable    */
#define F_DISABLE                0            /**< state disable   */



/*************************************************
 * @brief Minium value of base type
 ************************************************/
#define FUINT8_MIN      0                      /**< Minium value of UINT8  */
#define FUINT16_MIN     0                      /**< Minium value of UINT16 */
#define FUINT32_MIN     0                      /**< Minium value of UINT32 */
#define FUINT64_MIN     0                      /**< Minium value of UINT64 */

#define FSINT8_MIN      0x80                   /**< Minium value of SINT8  */
#define FSINT16_MIN     0x8000                 /**< Minium value of SINT16 */
#define FSINT32_MIN     0x80000000             /**< Minium value of SINT32 */
#define FSINT64_MIN     0x8000000000000000     /**< Minium value of SINT64 */



/*************************************************
 * @brief Maximum value of base type
 ************************************************/
#define FUINT8_MAX      0xFF                   /**< Maxium value of UINT8  */
#define FUINT16_MAX     0xFFFF                 /**< Maxium value of UINT16 */
#define FUINT32_MAX     0xFFFFFFFF             /**< Maxium value of UINT32 */
#define FUINT64_MAX     0xFFFFFFFFFFFFFFFF     /**< Maxium value of UINT64 */

#define FSINT8_MAX      0x7F                   /**< Maxium value of SINT8  */
#define FSINT16_MAX     0x7FFF                 /**< Maxium value of SINT16 */
#define FSINT32_MAX     0x7FFFFFFF             /**< Maxium value of SINT32 */
#define FSINT64_MAX     0x7FFFFFFFFFFFFFFF     /**< Maxium value of SINT64 */



/*************************************************
 * @brief Define NULL
 ************************************************/
#ifndef NULL
#define NULL    ((void*)0)          /**< Empty pointer */
#endif

#ifndef F_NULL
#define F_NULL  ((void*)0)          /**< Empty pointer */
#endif



/*************************************************
 * @brief Compiler related definition
 ************************************************/
#if defined(__CC_ARM) || defined(__CLANG_ARM)     /**< ARM Compiler           */
    #include <stdarg.h>
    #define F_SECTION(x)               __attribute__((section(x)))
    #define F_UNUSED                   __attribute__((unused))
    #define F_USED                     __attribute__((used))
    #define F_ALIGN(n)                 __attribute__((aligned(n)))
    /* ARM Compiler 6 (armclang) */
    #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 
        #define F_WEAK                     __attribute__((weak))
        #define F_PACKED                   __attribute__((__packed__))
    #else
        #define F_WEAK                     __weak
        #define F_PACKED                   __packed
    #endif /**< defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)  */
    #define F_INLINE                   static __inline
    #define F_RAM_FUNC
    #define F_NOINLINE                 __attribute__ ( (noinline) )

#elif defined (__IAR_SYSTEMS_ICC__)               /**< IAR Compiler           */
    #include <stdarg.h>
    #define F_SECTION(x)               @ x
    #define F_UNUSED
    #define F_USED                     __root
    #define F_ALIGN(n)                 PRAGMA(data_alignment=n)
    #define F_WEAK                     __weak
    #define F_PACKED                   __packed
    #define F_INLINE                   static inline
    #define F_RAM_FUNC                 __ramfunc
    #define F_NOINLINE                 _Pragma("optimize = no_inline")

#elif defined (__GNUC__)                          /**< GNU GCC Compiler       */
    #ifdef F_USING_GUN_NEWLIB
        #include <stdarg.h>
    #else
        /* the version of GNU GCC must be greater than 4.x */
        typedef __builtin_va_list      __gnuc_va_list;
        typedef __gnuc_va_list         va_list;
        #define va_start(v,l)          __builtin_va_start(v,l)
        #define va_end(v)              __builtin_va_end(v)
        #define va_arg(v,l)            __builtin_va_arg(v,l)
    #endif

    #define F_SECTION(x)               __attribute__((section(x)))
    #define F_UNUSED                   __attribute__((unused))
    #define F_USED                     __attribute__((used))
    #define F_ALIGN(n)                 __attribute__((aligned(n)))
    #define F_WEAK                     __attribute__((weak))
    #define F_PACKED                   __attribute__((__packed__))
    #define F_INLINE                   static __inline
    #define F_RAM_FUNC                 __attribute__((section(".RamFunc")))
    #define F_NOINLINE                 __attribute__ ( (noinline) )

#elif defined (__ADSPBLACKFIN__)                  /**< VisualDSP++ Compiler   */
    #include <stdarg.h>
    #define F_SECTION(x)               __attribute__((section(x)))
    #define F_UNUSED                   __attribute__((unused))
    #define F_USED                     __attribute__((used))
    #define F_ALIGN(n)                 __attribute__((aligned(n)))
    #define F_WEAK                     __attribute__((weak))
    #define F_PACKED
    #define F_INLINE                   static inline
    #define F_RAM_FUNC
    #define F_NOINLINE

#elif defined (_MSC_VER)
    #include <stdarg.h>
    #define F_SECTION(x)
    #define F_UNUSED
    #define F_USED
    #define F_ALIGN(n)                 __declspec(align(n))
    #define F_WEAK
    #define F_PACKED
    #define F_INLINE                   static __inline
    #define F_RAM_FUNC
    #define F_NOINLINE

#else
    #error unsupported tool chain
#endif



/*************************************************
 * @brief Alignment macro define
 ************************************************/
#define F_GET_ALIGN_DOWN(a, size) (a & (~(size-1)))
#define F_GET_ALIGN_UP(a, size)   ((a+size-1) & (~ (size-1)))
#define F_GET_ALIGN(a, size)      F_GET_ALIGN_DOWN(a, size)



/*************************************************
 * @brief Number size judgment macro define
 ************************************************/
#define F_MAX(a,b)     ((a) > (b) ? (a) : (b)) /**< Get bigger  */
#define F_MIN(a,b)     ((a) > (b) ? (b) : (a)) /**< Get smaller */




/* Exported define end ********************************************************/

/* Exported typedef start *****************************************************/

/*************************************************
 * @brief Type define sample
 ************************************************/
typedef enum enumName
{
    F_R_OK = 0U,           /**< Arg brief */
    F_R_ERR = -1U,           /**< Arg brief */
    F_R_TIMEOUT = -2U,           /**< Arg brief */
}f_ret_e;



/* Exported typedef end *******************************************************/

/* Exported variable start ****************************************************/





/* Exported variable end ******************************************************/

/* Exported function start ****************************************************/





/* Exported function end ******************************************************/

/** C++ compatible */
#ifdef __cplusplus
}
#endif /** __cplusplus */

#endif /** __FINS_DEFINE_H */
////////////////////////////////- END OF FILE  -////////////////////////////////
